package com.lzq.mq.broker.config;

import com.lzq.mq.broker.cache.CommonCache;
import com.lzq.mq.common.constants.BrokerConstants;
import com.lzq.mq.common.enums.BrokerClusterModeEnum;
import com.lzq.mq.common.utils.AssertUtils;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

/**
 * 全局配置加载器, 用于加载全局配置, 并写入到CommonCache中
 */
@Slf4j
public class GlobalPropertiesLoader {

    /**
     * 加载全局配置
     */
    public void loadProperties() {
        GlobalProperties globalProperties = new GlobalProperties();
        // String mqHome = System.getenv(BrokerConstants.EAGLE_MQ_HOME);
        String mqHome = System.getenv(BrokerConstants.MQ_HOME);
        AssertUtils.isNotBlank(mqHome, "请设置环境变量:EAGLE_MQ_HOME");
        String brokerMode = System.getenv(BrokerConstants.BROKER_MODE);
        AssertUtils.isNotBlank(brokerMode, "请设置环境变量:BROKER_MODE");
        AssertUtils.isTrue(!BrokerClusterModeEnum.exist(brokerMode), "不支持的Broker集群模式");

        globalProperties.setMqHome(mqHome);
        globalProperties.setBrokerMode(brokerMode);
        CommonCache.setGlobalProperties(globalProperties);

        String brokerHome = mqHome + BrokerConstants.BROKER_FILE_DIR + BrokerConstants.SPLIT + brokerMode;
        CommonCache.getGlobalProperties().setBrokerHome(brokerHome);

        // 通过配置文件导入properties
        String propertyFilePath = brokerHome + BrokerConstants.BROKER_PROPERTY_FILE_PATH;
        Properties properties = new Properties();
        try {
            properties.load(Files.newInputStream(Paths.get(propertyFilePath)));
        } catch (IOException e) {
            log.error("加载配置文件失败:{}", propertyFilePath);
            throw new RuntimeException(e);
        }

        // 设置属性
        globalProperties.setNameServerIp(properties.getProperty(BrokerConstants.BROKER_PROPERTY_KEY_NAMESERVER_IP));
        globalProperties.setNameServerPort(Integer.parseInt(properties.getProperty(BrokerConstants.BROKER_PROPERTY_KEY_NAMESERVER_PORT)));
        globalProperties.setNameServerUser(properties.getProperty(BrokerConstants.BROKER_PROPERTY_KEY_NAMESERVER_USER));
        globalProperties.setNameServerPassword(properties.getProperty(BrokerConstants.BROKER_PROPERTY_KEY_NAMESERVER_PASSWORD));
        globalProperties.setBrokerPort(Integer.parseInt(properties.getProperty(BrokerConstants.BROKER_PROPERTY_KEY_BROKER_PORT)));
        globalProperties.setReBalanceStrategy(properties.getProperty(BrokerConstants.BROKER_PROPERTY_KEY_REBALANCE_STRATEGY));

        globalProperties.setBrokerClusterGroup(properties.getProperty(BrokerConstants.BROKER_PROPERTY_KEY_BROKER_CLUSTER_GROUP));
        globalProperties.setBrokerClusterMode(properties.getProperty(BrokerConstants.BROKER_PROPERTY_KEY_BROKER_CLUSTER_MODE));
        globalProperties.setBrokerClusterRole(properties.getProperty(BrokerConstants.BROKER_PROPERTY_KEY_BROKER_CLUSTER_ROLE));
        log.info("加载全局配置成功:{}", globalProperties);
    }
}
